<?php

if (! function_exists('jsonResponse')){

    function jsonResponse($message, $code = 0, $status = 'error')
    {
        return response()->json([
            'status' => $status,
            'code' => $code,
            'msg' => $message
        ],$code);
    }
}

if (! function_exists('customResponse')){

    function customResponse($message, $code = 0, $status = 'error')
    {
        return response()->json([
            'status' => $status,
            'code' => $code,
            'msg' => $message
        ],$code);
    }

}





if (! function_exists('beautifyChapterName')){

    function beautifyChapterName($name,$limit = 11){


        if (mb_strlen($name) <= $limit){
            return $name;
        }

        return mb_substr($name,0,$limit).'...';

    }

}


if (! function_exists('thisDayStartTime')){

    /**
     * 获取当天开始的时间
     */
    function thisDayStartTime(){
        return mktime(0, 0 , 0,date("m"),date("d"),date("Y"));
    }
}

if (! function_exists('thisWeekStartTime')){

    /**
     * 获取本周开始的时间
     */
    function thisWeekStartTime(){
        return mktime(0, 0 , 0,date("m"),date("d")-date("w")+1,date("Y"));
    }
}

if (! function_exists('lastWeekStartTime')){

    /**
     * 获取本周开始的时间
     */
    function lastWeekStartTime(){
        return mktime(0, 0 , 0,date("m"),date("d")-date("w")+1-7,date("Y"));
    }
}



if (! function_exists('thisMonthStartTime')){

    /**
     * @return false|int
     * 获取本月开始的时间
     */
    function thisMonthStartTime(){
        return mktime(0, 0 , 0,date("m"),1,date("Y"));
    }
}


/**
 * 将换行符切换为br
 * @param $content
 * @return mixed
 */
function placeBr($content){

    $order = array("\r\n", "\n", "\r");
    $replace = '<br />';
    $text=str_replace($order, $replace, $content);

    return $text;

}

function placeTextrn2n($content){
    $order = array("\r\n", "\n", "\r");
    $replace = "\n";
    $text = str_replace($order,$replace,$content);
    return $text;

}

function nextChapter($chapter){

    $book = $chapter->book;

    $next = $book->chapters()->where('id','>',$chapter->id)->where('status',1)->first();

    if (empty($next)){
        return null;
    }

    return $next;
}

function prevChapter($chapter){

    $book = $chapter->book;

    $next = $book->chapters()->where('id','<',$chapter->id)->where('status',1)->orderBy('id','desc')->first();

    if (empty($next)){
        return null;
    }

    return $next;

}